package com.lanqiao.gxnushop.controller;

import com.lanqiao.gxnushop.entity.Buyers;
import com.lanqiao.gxnushop.entity.Sellers;
import com.lanqiao.gxnushop.entity.UserLogins;
import com.lanqiao.gxnushop.service.BuyersService;
import com.lanqiao.gxnushop.service.SellersService;
import com.lanqiao.gxnushop.service.UserLoginsService;
import com.lanqiao.gxnushop.util.AccountUtil;
import com.lanqiao.gxnushop.util.UUIdUtil;
import com.lanqiao.gxnushop.valueObject.RegisterCount;
import com.lanqiao.gxnushop.valueObject.yhClass;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
public class UserLoginsController {
    @Autowired
    private UserLoginsService userLoginsService;
    @Autowired
    private SellersService sellersService;
    @Autowired
    private BuyersService buyersService;

    @Operation(summary = "用于注册时生成账号 无需传入参数,注册页面挂载前先请求，返回值为account")
    @GetMapping("/loginCtrl/getAccount")
    public String getAccount(){
        String account = AccountUtil.getAccount();
        return account;
    }

    @Operation(summary = "用户注册   传入的参数有：userLogins:account,password,yhsf(用户身份,0管理员,1商家，2买家)")
    @Parameter(name = "userLogins",description = "用户注册信息")
    @PostMapping("/longinCtrl/add")
    public boolean add(UserLogins userLogins){
        //生成userId作为主键
        String id = UUIdUtil.getUserId();
        userLogins.setUserId(id);
        //拿到想要注册的账户是  1:卖家 还是 2:买家  将注册信息写入卖家/买家的数据库
        String yhsf = userLogins.getYhsf();
        if(yhsf.equals("1")){
            Sellers sellers = new Sellers();
            sellers.setSellerId(id);
            sellersService.addSeller(sellers.getSellerId());
        }else if(yhsf.equals("2")){
            Buyers buyers = new Buyers();
            buyers.setBuyerId(id);
            buyersService.addBuyer(buyers.getBuyerId());
        }
        boolean res = userLoginsService.add(userLogins);
        return res;
    }

    @Operation(summary = " 用户删除 用户传入的参数有userId")
    @Parameter(name = "userId",description = "用户编号")
    @DeleteMapping("/loginCtrl/delete")
    public boolean delete(String userId){
        //如果要删除的用户在买家表中，删除买家表中的信息
        if(buyersService.findById(userId)!=null){
            return buyersService.delete(userId);
        }else{
            return sellersService.delete(userId);
        }
    }

    @Operation(summary = "用户修改密码,传入的userLogins中需要有userId和password")
    @Parameter(name = "userLogins",description = "用户登录信息")
    @PostMapping("/loginCtrl/updatePassword")
    public boolean update(UserLogins userLogins){
        return userLoginsService.update(userLogins);
    }

    @Operation(summary = "用户登录")
    @Parameter(name = "userLogins",description = "用户登录信息")
    @GetMapping("/loginCtrl/login")
    public UserLogins login(UserLogins userLogins){
        return userLoginsService.select(userLogins);
    }

    @Operation(summary = "近 6 个月 每个月买家的注册量 ")
    @GetMapping("/loginCtrl/buyerCount")
    public ArrayList<RegisterCount> count(){
        return userLoginsService.count();
    }

    @Operation(summary = "近 6 个月 每个月卖家的注册量 ")
    @GetMapping("/loginCtrl/sellerCount")
    public ArrayList<RegisterCount> countSeller(){
        return userLoginsService.countSeller();
    }

    @Operation(summary = "用户类型及数量")
    @GetMapping("/loginCtrl/getYhClassCount")
    public ArrayList<yhClass> getYhClassCount(){
        return userLoginsService.getYhClassCount();
    }
}
